package com.task.core.service.impl;


import com.task.common.core.redis.RedisCache;
import com.task.common.enums.*;
import com.task.common.utils.SystemFunctionUtil;
import com.task.core.domain.DataOperationInfo;
import com.task.core.domain.DataPreAvailableInfo;
import com.task.core.domain.MissionInfo;
import com.task.core.domain.info.DataAvailableDataInfo;
import com.task.core.domain.info.SourceConfig;
import com.task.core.mapper.DataOperationInfoMapper;
import com.task.core.mapper.DataPreAvailableMapper;
import com.task.core.service.DataPreAvailableService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class DataPreAvailableServiceImpl implements DataPreAvailableService {

    @Resource
    private DataPreAvailableMapper dataPreAvailableMapper;

    @Resource
    private DataOperationInfoMapper dataOperationInfoMapper;

    @Resource
    private RedisCache redisCache;


    /**
     * 初始化预计算表
     *
     * @param childTableCountMap 每张子表的数量
     * @param dataSourceCode     数据源
     * @param allCount           这个数据源总共的数量
     */
    @Override
    public void initPreAvailable(Map<String, Long> childTableCountMap, String dataSourceCode, Integer allCount,String opraterId) {
        if(!ObjectUtils.isEmpty(childTableCountMap)){

//            List<DataPreAvailableInfo> list = dataPreAvailableMapper.selectPreAvailableBySourceCode(dataSourceCode);
//
//            //插入
//            if(ObjectUtils.isEmpty(list)){
//                list = new ArrayList<>();
//
//                for (Map.Entry<String, Long> entry : childTableCountMap.entrySet()) {
//                    DataPreAvailableInfo dataPreAvailableInfo = new DataPreAvailableInfo();
//                    dataPreAvailableInfo.setTableId(entry.getKey());
//                    dataPreAvailableInfo.setAffiliateAvailableCount(entry.getValue());
//                    dataPreAvailableInfo.setAdvertiserAvailableCount(entry.getValue());
//                    dataPreAvailableInfo.setMissionAvailableCount(entry.getValue());
//                    dataPreAvailableInfo.setCode(UUID.randomUUID().toString().replace("-",""));
//                    dataPreAvailableInfo.setCreateUser(Long.valueOf(opraterId));
//                    dataPreAvailableInfo.setSourceCode(dataSourceCode);
//                    //1代表初始化
//                    dataPreAvailableInfo.setMissionIsInit(PreIsInitFlagEnum.MISSION.getIsInit());
//                    dataPreAvailableInfo.setAffiliateIsInit(PreIsInitFlagEnum.AFFILIATE.getIsInit());
//                    dataPreAvailableInfo.setAdvertiserIsInit(PreIsInitFlagEnum.ADVERTISER.getIsInit());
//                    list.add(dataPreAvailableInfo);
//                }
//
//                dataPreAvailableMapper.initPreAvailable(list);
//            }else {
//                //更新
//                for (DataPreAvailableInfo dataPreAvailableInfo : list) {
//                    String tableId = dataPreAvailableInfo.getTableId();
//                    dataPreAvailableInfo.setMissionAvailableCount(dataPreAvailableInfo.getMissionAvailableCount()+childTableCountMap.get(tableId));
//                    dataPreAvailableInfo.setAdvertiserAvailableCount(dataPreAvailableInfo.getAdvertiserAvailableCount()+childTableCountMap.get(tableId));
//                    dataPreAvailableInfo.setAffiliateAvailableCount(dataPreAvailableInfo.getAffiliateAvailableCount()+childTableCountMap.get(tableId));
//                    dataPreAvailableMapper.updatePreAvailable(dataPreAvailableInfo);
//                }
//
//
//
//
//            }

        }
    }

    /**
     * 通过数据源获取可以使用的子表的后缀
     *
     * @param sourceCodeList
     * @param missionCode
     * @param affiliateCode
     * @param advertiserCode

     * @return
     */
    @Override
    public DataPreAvailableInfo getCanUseChildTableIdBySourceCodes(List<String> sourceCodeList, String missionCode, String affiliateCode, String advertiserCode, String missionFlag, String affiliateString ,String advertiserFlag) {
        DataPreAvailableInfo dataPreAvailableInfo = dataPreAvailableMapper.getCanUseChildTableSuffixRandomBySourceCodes(sourceCodeList,missionCode,affiliateCode,advertiserCode,missionFlag,affiliateString,advertiserFlag);
        return dataPreAvailableInfo;
    }

    /**
     * 更新预计算表
     *
     * @param dataAvailableDataInfo
     * @param dataType
     */
    @Override
    public void updateOrInsertPreAvailableData(DataAvailableDataInfo dataAvailableDataInfo, String missionCode, String affiliateCode, String advertiserCode, String dataType) {
        if(DataFileTypeEnum.DATA.name().equals(dataType)){
            if(!ObjectUtils.isEmpty(dataAvailableDataInfo) || !ObjectUtils.isEmpty(dataAvailableDataInfo.getDataPreAvailableInfo())){
                dataAvailableDataInfo.setMissionCode(missionCode);
                dataAvailableDataInfo.setAdvertiserCode(advertiserCode);
                dataAvailableDataInfo.setAffiliateCode(affiliateCode);
                DataOperationInfo dataOperationInfo = dataAvailableDataInfo.getDataOperationInfo();


                //查询资料预计算表中是否已存在使用记录
                DataPreAvailableInfo dataPreAvailableInfo =  dataPreAvailableMapper.selectPreAvailableBySourceCodeAndDuplicateCondition(
                        dataOperationInfo.getDataSourceCode(),
                        missionCode,
                        affiliateCode,
                        advertiserCode);

                //如果资料预计表中已存在使用记录就做更新 ;否则直接进行插入操作
                if(!ObjectUtils.isEmpty(dataPreAvailableInfo)){
                    //资料预计算表每使用一次数量加一
                    dataPreAvailableInfo.setMissionUseCount(dataPreAvailableInfo.getMissionUseCount()+1);
                    dataPreAvailableInfo.setAffiliateUseCount(dataPreAvailableInfo.getAffiliateUseCount()+1);
                    dataPreAvailableInfo.setAdvertiserUseCount(dataPreAvailableInfo.getAdvertiserUseCount()+1);
                    dataPreAvailableMapper.updatePreAvailable(dataPreAvailableInfo);
                }else {
                    DataPreAvailableInfo preAvailableInfo = new DataPreAvailableInfo();
                    preAvailableInfo.setCode(UUID.randomUUID().toString().replace("-",""));
                    preAvailableInfo.setMissionCode(missionCode);
                    preAvailableInfo.setMissionUseCount(1L);
                    preAvailableInfo.setAffiliateCode(affiliateCode);
                    preAvailableInfo.setAffiliateUseCount(1L);
                    preAvailableInfo.setAdvertiserCode(advertiserCode);
                    preAvailableInfo.setAdvertiserUseCount(1L);
                    preAvailableInfo.setSourceCode(dataOperationInfo.getDataSourceCode());
                    dataPreAvailableMapper.insertPreUseInfo(preAvailableInfo);

                }
            }
        }


    }

    @Override
    public DataOperationInfo getCanUseSendRefere(SourceConfig sourceConfig) {
        DataOperationInfo dataOperationInfo = null;
        List<String> sourceCodeList = sourceConfig.getSourceCodeList();
        for (int i = 1; i <=20; i++) {
           String tableRefereName =  SystemFunctionUtil.REFERER_TABLE_PREFIX_INFO+i;
            dataOperationInfo  =   dataOperationInfoMapper.getCanUseSendRefere(tableRefereName,sourceCodeList);
            if (!ObjectUtils.isEmpty(dataOperationInfo)){
                break;
            }
        }

        return dataOperationInfo;
    }

    @Override
    public void updatePreAvailableDataIncrease(String duplicateCondition, String sourceCode, MissionInfo missionByCode, String dataType) {
        if(DataFileTypeEnum.DATA.name().equals(dataType)){
            if(DuplicateConditionEnum.BY_MISSION_ID.name().equals(duplicateCondition)){
                dataPreAvailableMapper.updatePreAvailableDataIncrease(sourceCode,missionByCode.getCode(),DuplicateConditionEnum.BY_MISSION_ID.name(),null,null);
            }else if(DuplicateConditionEnum.BY_AFFILIATE_ID.name().equals(duplicateCondition)){
                dataPreAvailableMapper.updatePreAvailableDataIncrease(sourceCode,missionByCode.getAffiliateCode(),null,DuplicateConditionEnum.BY_AFFILIATE_ID.name(),null);
            }else if(DuplicateConditionEnum.BY_ADVERTISER_ID.name().equals(duplicateCondition)){
                dataPreAvailableMapper.updatePreAvailableDataIncrease(sourceCode,missionByCode.getAdvertiserCode(),null,null,DuplicateConditionEnum.BY_ADVERTISER_ID.name());
            }
        }

    }

    @Override
    public DataPreAvailableInfo getUseDataCountByFilterAndSourceCode(MissionInfo missionByCode, String sourceCode) {
        return dataPreAvailableMapper.getUseDataCountByFilterAndSourceCode(missionByCode,sourceCode);
    }

    @Override
    public void updateUseBySourceCode(String datasourceCode) {
       List<DataPreAvailableInfo>  dataPreAvailableInfos = dataPreAvailableMapper.getPreAvailableBySourceCode(datasourceCode);
        if(!ObjectUtils.isEmpty(dataPreAvailableInfos)){
            dataPreAvailableInfos.forEach(dataPreAvailableInfo -> {
                dataPreAvailableInfo.setMissionUseCount(0L);
                dataPreAvailableInfo.setAffiliateUseCount(0L);
                dataPreAvailableInfo.setAdvertiserUseCount(0L);
                dataPreAvailableMapper.updatePreAvailableByCode(dataPreAvailableInfo);
            });
       }
    }

    @Override
    public void deletePreAvailableData(String datasourceCode) {
        dataPreAvailableMapper.deletePreAvailableData(datasourceCode);
    }

//    @Override
//    public void updataAvailableDataBatchByIsInitAndSouceCode(List<DataAvailableDataInfo> dataAvailableDataInfos) {
//        List<DataPreAvailableInfo> list = new ArrayList<>();
//
//        for (DataAvailableDataInfo dataAvailableDataInfo : dataAvailableDataInfos) {
//            String duplicateCondition = dataAvailableDataInfo.getDuplicateCondition();
//            DataPreAvailableInfo dataPreAvailableInfo = dataAvailableDataInfo.getDataPreAvailableInfo();
//            DataOperationInfo dataOperationInfo = dataAvailableDataInfo.getDataOperationInfo();
//
//
//            //按照任务Id
//            if(DuplicateConditionEnum.BY_MISSION_ID.name().equals(duplicateCondition) ){
//                if ( dataPreAvailableInfo.getIsInit().equals(PreTableIsInItEnum.NOT_INIT.getIndex()) ){
//                    dataPreAvailableInfo.setIsInit(PreTableIsInItEnum.INIT.getIndex());
//                    dataPreAvailableInfo.setMissionCode(dataAvailableDataInfo.getMissionCode());
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }else {
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }
//
//                //按照联盟id
//            }else if(DuplicateConditionEnum.BY_AFFILIATE_ID.name().equals(duplicateCondition)){
//                if ( dataPreAvailableInfo.getIsInit().equals(PreTableIsInItEnum.NOT_INIT.getIndex()) ){
//                    dataPreAvailableInfo.setIsInit(PreTableIsInItEnum.INIT.getIndex());
//                    dataPreAvailableInfo.setMissionCode(dataAvailableDataInfo.getAffiliateCode());
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }else {
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }
//                //按照广告主Id
//            }else if(DuplicateConditionEnum.BY_ADVERTISER_ID.name().equals(duplicateCondition)){
//                if ( dataPreAvailableInfo.getIsInit().equals(PreTableIsInItEnum.NOT_INIT.getIndex()) ){
//                    dataPreAvailableInfo.setIsInit(PreTableIsInItEnum.INIT.getIndex());
//                    dataPreAvailableInfo.setMissionCode(dataAvailableDataInfo.getAdvertiserCode());
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }else {
//                    dataPreAvailableInfo.setMissionCode(dataOperationInfo.getDataSourceCode());
//                }
//            }
//            list.add(dataPreAvailableInfo);
//        }
//
//        dataPreAvailableMapper.updataAvailableDataBatchByIsInitAndSouceCode(list);
//    }


}
